﻿using Microsoft.EntityFrameworkCore;
using System.IO;

namespace 一对多
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                /*Article a = new Article();
                a.Title = "A1";
                a.Content = "B1";
                Comment comment = new Comment() { Message = "11" };
                Comment comment1 = new Comment() { Message = "21" };
                a.Comments.Add(comment);
                a.Comments.Add(comment1);
                ctx.Articles.Add(a);
                ctx.SaveChanges();*/
                //通过查询文章查询评论
                /*Article a = ctx.Articles.Include(a => a.Comments).Single(a => a.Id == 1);
                Console.WriteLine(a.Id);
                Console.WriteLine(a.Content);
                foreach (var comment in a.Comments)
                {
                    Console.WriteLine(comment.Message);
                }*/
                //通过查询评论查询文章
                /*Comment c = ctx.Comments.Include(c => c.Article).Single(a => a.Id == 2);
                Console.WriteLine(c.Message);
                Console.WriteLine(c.Article.Id);
                Console.WriteLine(c.Article.Content);*/
                //查询指定的字段
                /*var a1 = ctx.Articles.Select(c => new { c.Id, c.Title }).First();
                Console.WriteLine(a1.Title);*/
                //额外的外键字段
                /*var a2 = ctx.Comments.Select(c => new { c.Id, c.ArticleId }).Single(a => a.Id == 2);
                Console.WriteLine(a2.Id + "-" + a2.ArticleId);*/
                //单向导航属性
                /*User u1 = new User { Name = "许子正" };
                Leave leave1 = new Leave();
                leave1.Requester = u1;
                leave1.Remarks = "家里三套房拆迁，回家处理";
                ctx.Users.Add(u1);
                ctx.Leaves.Add(leave1);
                ctx.SaveChanges();*/
                /*User u = ctx.Users.Single(u => u.Name == "许子正");
                foreach (var l in ctx.Leaves.Where(l => l.Requester == u))
                {
                    Console.WriteLine(l.Remarks);
                }*/
                //QueryBooks("test", true, true, 1);

                /*ctx.Database.ExecuteSqlInterpolatedAsync(@$"insert into T_Artcles(Title,Content)
select Title, Content from T_Artcles");*/

                /*string title = "%test%";
                var queryable = ctx.Articles.FromSqlInterpolated($"select * from T_Artcles where Title like {title}");
                foreach (var item in queryable.Include(a => a.Comments).OrderBy(a => Guid.NewGuid()).Skip(1).Take(2))
                {
                    Console.WriteLine(item.Title);
                    foreach (var item1 in item.Comments)
                    {
                        Console.WriteLine(item1.Message);
                    }
                }*/

                var a = ctx.Articles.First();
                a.Title = "test";
                ctx.SaveChanges();
                //直接update更新
                Article a1 = new Article { Id = 2, Title = "test" };
                var entry = ctx.Entry(a);
                entry.Property("Title").IsModified = true;
                Console.WriteLine(entry.DebugView.LongView);
                ctx.SaveChanges();

                //ctx.Articles.IgnoreQueryFilters().Where(b => b.Title.Contains("o")).ToArray()
            }
        }

        private static void PrintPage(int page, int pagesize = 10)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                IQueryable<Article> articles = ctx.Articles;
                IQueryable<Article> item = articles.Skip((page - 1) * pagesize).Take(pagesize);
                foreach (var item1 in item)
                {
                    Console.WriteLine(item1.Title);
                }
                long count = articles.Count();
                long total = (long)Math.Ceiling(count * 1.0 / pagesize);
                Console.WriteLine("总页数" + total);
            }
        }

        private static void QueryBooks(string searchWords, bool searchAll, bool orderByPrice, double upperPrice)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                IQueryable<Article> articles = ctx.Articles;
                if (searchAll)//匹配书名或、作者名
                {
                    articles = articles.Where(b => b.Title.Contains(searchWords));
                }
                else//只匹配书名
                {
                    articles = articles.Where(b => b.Title.Contains(searchWords));
                }
                if (orderByPrice)//按照价格排序
                {
                    articles = articles.OrderBy(b => b.Id);
                }
                foreach (Article b in articles)
                {
                    Console.WriteLine($"{b.Id},{b.Title},{b.Title}");
                }
            }
        }

    }
}
